package com.qnw.CardGroupManagement.adapter;

import android.content.*;
import android.view.*;
import android.widget.*;

import com.qnw.CardGroupManagement.*;
import com.qnw.CardGroupManagement.management.*;
import com.ygo.feihua.*;

import java.util.*;

public class AdapterCardList extends BaseAdapter
{

	private List<Card> data;
	private Zujian zujian;
	private Gongju gj;
	private Context context;

	private boolean selected=false;
	
	CdbManagement cm;
	
	public AdapterCardList(Context context, List<Card> data){
		this.context=context;
		this.data=data;
		gj=Gongju.getdx(context);
		cm=CdbManagement.getdx();
	}

	public void setData(List<Card> cardList){
		this.data.clear();
		for(Card card:cardList){
			this.data.add(card);
		}
	}
	
	
	public void setSelected(boolean selected)
	{
		this.selected = selected;
		notifyDataSetChanged();
	}

	public boolean isSelected()
	{
		return selected;
		
	}
	public void sx(List<Card> data){
		this.data=data;
		notifyDataSetInvalidated();
	}
	public void add(Card map){
		data.add(map);
		notifyDataSetChanged();
	}
	public void del(Card card){
		for(int i=0;i<data.size();i++){
			if(data.get(i).equals(card)){
				data.remove(i);
				notifyDataSetChanged();
				return;
			}
		}
	}
	
	
	
	class Zujian{
		TextView ac_name,ac_km;
		CheckBox ac_ch;
	}

	public List<Card> getdata(){
		return data;
	}

	@Override
	public int getCount()
	{
		// TODO: Implement this method
		return data.size();
	}

	@Override
	public Object getItem(int p1)
	{
		// TODO: Implement this method
		return data.get(p1);
	}

	@Override
	public long getItemId(int p1)
	{
		// TODO: Implement this method
		return p1;
	}

	@Override
	public View getView(final int position, View p2, ViewGroup p3)
	{
		if(p2==null){
			zujian=new Zujian();
			p2=LayoutInflater.from(context).inflate(R.layout.item_cardlist,null);
			zujian.ac_name=(TextView) p2.findViewById(R.id.ac_name);
			zujian.ac_km=(TextView) p2.findViewById(R.id.ac_km);
			zujian.ac_ch=(CheckBox) p2.findViewById(R.id.ac_ch);
			
			p2.setTag(zujian);
		}else{
			zujian=(Zujian) p2.getTag();
		}
		String km=data.get(position).getId();
		zujian.ac_name.setText(data.get(position).getName());
		zujian.ac_km.setText(km);
		if (selected) {
			zujian.ac_ch.setVisibility(View.VISIBLE);
			if (cm.conCard(data.get(position).getId())) {
				zujian.ac_ch.setChecked(true);
			} else {
				zujian.ac_ch.setChecked(false);
			}

		} else {
			cm.getXzcard().clear();
			zujian.ac_ch.setVisibility(View.GONE);
		}
		// TODO: Implement this method
		return p2;
	}



}
